package com.example.chapter07_client;

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.example.chapter07_client.Constant.DatabaseConstant;
import com.example.chapter07_client.Constant.ToastConstant;
import com.example.chapter07_client.Util.ToastUtil;
import com.example.chapter07_client.entity.User;

import java.time.LocalDateTime;

public class ContentWriteActivity extends AppCompatActivity implements View.OnClickListener {
    public final String TAG = "liu";
    private EditText et_name;
    private EditText et_age;
    private EditText et_height;
    private EditText et_weight;
    private CheckBox ck_married;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content_write);

        et_name = findViewById(R.id.et_name);
        et_age = findViewById(R.id.et_age);
        et_height = findViewById(R.id.et_height);
        et_weight = findViewById(R.id.et_weight);
        ck_married = findViewById(R.id.ck_married);

        findViewById(R.id.btn_save).setOnClickListener(this);
        findViewById(R.id.btn_read).setOnClickListener(this);
        findViewById(R.id.btn_delete).setOnClickListener(this);

    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @SuppressLint("Range")
    @Override
    public void onClick(View v) {
        String name = et_name.getText().toString();
        String age = et_age.getText().toString();
        String height = et_height.getText().toString();
        String weight = et_weight.getText().toString();
        boolean married = ck_married.isChecked();
        if(v.getId() == R.id.btn_save){
            ContentValues values = new ContentValues();
            values.put(UserInfoContent.USER_NAME,name);
            values.put(UserInfoContent.USER_AGE,Integer.parseInt(age));
            values.put(UserInfoContent.USER_HEIGHT,Float.parseFloat(height));
            values.put(UserInfoContent.USER_WEIGHT,Float.parseFloat(weight));
            values.put(UserInfoContent.USER_MARRIED,married);
            values.put(UserInfoContent.UPDATE_TIME, String.valueOf(LocalDateTime.now()));

            getContentResolver().insert(UserInfoContent.CONTENT_URI,values);
            ToastUtil.show(this,"保存成功");
        }

        if(v.getId() == R.id.btn_read){
            Cursor cursor = getContentResolver().query(UserInfoContent.CONTENT_URI, null, null,
                    null, null);
            if(cursor!=null){
                while (cursor.moveToNext()){
                    User info = new User();
                    info.id = cursor.getInt(cursor.getColumnIndex(UserInfoContent._ID));
                    info.name = cursor.getString(cursor.getColumnIndex(UserInfoContent.USER_NAME));
                    info.age = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_AGE));
                    info.height = cursor.getFloat(cursor.getColumnIndex(UserInfoContent.USER_HEIGHT));
                    info.weight = cursor.getFloat(cursor.getColumnIndex(UserInfoContent.USER_WEIGHT));
                    info.married = cursor.getInt(cursor.getColumnIndex(UserInfoContent.USER_MARRIED)) == 1;
                    Log.d(TAG,info.toString());
                }
                cursor.close();
            }
        }

        if(v.getId() == R.id.btn_delete){
            /*删除单行*/
            /*Uri uri = ContentUris.withAppendedId(UserInfoContent.CONTENT_URI,2);
            int deleted = getContentResolver().delete(uri,null,null);*/

            //删除多行
            int deleted = getContentResolver().delete(UserInfoContent.CONTENT_URI, "name=? and age=? and height=?" +
                    "and weight=? and married=?", new String[]{name, age, height, weight,
                    String.valueOf(married ? 1 : 0)});
            if(deleted > 0){
                ToastUtil.show(this,ToastConstant.DELETE_SUCCESS);
                Log.d(TAG, DatabaseConstant.DELETE_SUCCESS);
            }else {
                ToastUtil.show(this, ToastConstant.DELETE_FAILED);
                Log.d(TAG,DatabaseConstant.DELETE_FAILED);
            }

        }
    }
}